//
// Created by Mzoro on 2022/3/8.
//

#include <stdio.h>
#include "../../demo1/utils/utils.h"

#define MAX_LENGTH 1000

/**
 * 如果 line 字符串中包含 sub 子串 ，返回index. 如果不包含，返回 -1
 * @param line
 * @param sub
 * @return  -1 or larger than -1
 */
int contain(char line[], char sub[]);

int main() {

    char sub[] = "ould";
    char line[MAX_LENGTH];
    int length;
    while ((length = getLine(line, MAX_LENGTH)) > 0) {
        if (contain(line, sub) > -1) {
            printf("%s", line);
        }
    }
}

int contain(char line[], char sub[]) {

    int find = 0;
    char current;
    int li = 0;

    char sCurrent;
    int si = 0;

    int index = -1;
    while ((current = line[li]) != '\0') {

        while ((sCurrent = sub[si]) != '\0' && sCurrent == current) {
            if (!find) {
                index = li;
                find = 1;
            }
            si++;
            li++;
            current = line[li];
        }
        if (sCurrent == '\0') {
            // 说明匹配了整个 sub
            return index;
        } else {
            // 说明没有完全匹配，继续向后查找line
            index = -1;
            li++;
            si = 0;
        }
    }

    return index;
}
